package com.baijiaoxi.common.constant;

import com.google.common.collect.ImmutableMap;

import java.util.Map;

public class FileSuffix {
    public static final int OTHER = 99;//不明格式
    public static final int FOLDER = 0;//文件夹
    public static final int PDF = 1;
    public static final int VIDEO = 2;
    public static final int PIC = 3;
    public static final int PPT = 4;
    public static final int DOC = 5;
    public static final int EXCEL = 6;
    public static final int RADIO = 7;
    public static final int ZIP = 8;

    public static final int OFFIACCOUNT_NEWS = 9;
    public static final Map<String, Integer> MAP = new ImmutableMap.Builder<String, Integer>()
            .put("pdf", PDF)
            .put("mp4", VIDEO)
            .put("avi", VIDEO)
            .put("rmvb", VIDEO)
            .put("m4v", VIDEO)
            .put("mov", VIDEO)
            .put("jpg", PIC)
            .put("png", PIC)
            .put("jpeg", PIC)
            .put("bmp",PIC)
            .put("jfif",PIC)
            .put("webp",PIC)
            .put("vis",PIC)
            .put("ppt", PPT)
            .put("pptx", PPT)
            .put("doc", DOC)
            .put("docx", DOC)
            .put("xls", EXCEL)
            .put("xlsx", EXCEL)
            .put("mp3", RADIO)
            .put("zip", ZIP)
            .put("rar", ZIP)
            .build();

    public static Integer getFileTypeBySuffix(String suffix) {
        Integer fileType = null;
        if (suffix != null && !suffix.isEmpty()) {
            if (suffix.startsWith("."))
                suffix = suffix.replace(".", "");
            suffix = suffix.toLowerCase();
            fileType = MAP.get(suffix);
            if (fileType == null) fileType = OTHER;
        }
        return fileType;
    }

    public static String getFileSuffix(String filename) {
        if (filename == null || filename.isEmpty()) {
            return null;
        }
        int lastDotIndex = filename.lastIndexOf('.');
        if (lastDotIndex == -1 || lastDotIndex == filename.length() - 1) {
            return ""; // 没有后缀名或文件名以.结尾
        }
        return filename.substring(lastDotIndex + 1).toLowerCase();
    }

}
